#####################################################################
#
#                          C++ Assistant
#
# 
# Author : Guillaume Delhumeau <guillaume.delhumeau@gmail.com>
# License : GNU General Public License 3
#
#####################################################################

import gedit
import os

def switch_to(window, uri):
	docs = window.get_documents() 
	for d in docs:
		if d.get_uri() == uri:
			tab = gedit.tab_get_from_document(d)
			window.set_active_tab(tab)
			return

	window.create_tab_from_uri(uri, None, 0, False, True) 

def switch_h_cpp(window):
	# Gettings docs
	doc = window.get_active_document() 
	doc_url = doc.get_uri()
	
	# It is a .h ?
	h_ext = doc_url[len(doc_url)-2:len(doc_url)]
	if h_ext == ".h":
		new_doc = doc_url[0:len(doc_url)-2] + ".cpp"
		if os.path.exists(new_doc.replace("file://", "")):
			switch_to(window, new_doc)
		return			
		
	# Is is a .cpp ?
	cpp_ext = doc_url[len(doc_url)-4:len(doc_url)]		
	if cpp_ext == ".cpp":
		new_doc = doc_url[0:len(doc_url)-4] + ".h"
		print new_doc
		if os.path.exists(new_doc.replace("file://", "")):
			switch_to(window, new_doc)
		return
			
		
	
	

